package com.zw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.dto.TemplateDto;
import com.zw.entity.Template;
import com.zw.entity.TemplateColumns;
import com.zw.mapper.TemplateColumnsMapper;
import com.zw.mapper.TemplateMapper;
import com.zw.service.TemplateService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TemplateService {

    @Resource
    private TemplateColumnsMapper templateColumnsMapper;

    @Override
    public List<TemplateColumns> templateColumns(Long id) {
        LambdaQueryWrapper<TemplateColumns> wq = new LambdaQueryWrapper<>();
        wq.eq(TemplateColumns::getTemplateId, id);
        wq.orderByAsc(TemplateColumns::getSort);
        List<TemplateColumns> templateColumns = templateColumnsMapper.selectList(wq);
        return templateColumns;
    }

    @Override
    public List<Template> selectTemplateList(Template template) {
        return baseMapper.selectTemplate(template);
    }

    @Transactional
    @Override
    public int insertTemplate(Template template) {
        int result = 0;
        result += baseMapper.insert(template);
        List<TemplateColumns> templateColumns = template.getTemplateColumns();
        if (!CollectionUtils.isEmpty(templateColumns)){
            templateColumns.forEach(cl -> cl.setTemplateId(template.getId()));
            templateColumnsMapper.insertTemplateColumns(templateColumns);
        }
        return result;
    }

    @Transactional
    @Override
    public int updateTemplate(Template template) {
        templateColumnsMapper.deleteByTemplateIds(List.of(template.getId()));
        List<TemplateColumns> templateColumns = template.getTemplateColumns();
        if (!CollectionUtils.isEmpty(templateColumns)){
            templateColumns.forEach(cl -> cl.setTemplateId(template.getId()));
            templateColumnsMapper.insertTemplateColumns(templateColumns);
        }
        return baseMapper.updateById(template);
    }

    @Transactional
    @Override
    public int removeTemplates(Long[] ids) {
        templateColumnsMapper.deleteByTemplateIds(Arrays.asList(ids));
        return baseMapper.deleteByIds(Arrays.asList(ids));
    }

    @Override
    public Template selectTemplateById(Long templateId) {
        List<Template> templates = baseMapper.selectTemplate(Template.builder().id(templateId).build());
        return CollectionUtils.isEmpty(templates) ? null : templates.get(0);
    }

    @Override
    public List<Template> visibleTemplateList(TemplateDto templateDto) {
        LambdaQueryWrapper<Template> wq = new LambdaQueryWrapper<>();
        wq.eq(Template::getStatus, Template.visible);
        wq.orderByAsc(Template::getCreateTime);
        return baseMapper.selectList(wq);
    }

    @Override
    public Template visibleTemplate(Long id) {
        List<Template> templates = baseMapper.selectTemplate(Template.builder().id(id).status(Template.visible).build());
        return CollectionUtils.isEmpty(templates) ? null : templates.get(0);
    }
}
